 /**
 * Description: 系统用户业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:19
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.common.beans.constant.PropertiesEnum;
import com.metro.paperless.sys.beans.vo.SysUserRole;
import com.metro.paperless.sys.mapper.ISysUserRoleMapper;
import com.metro.utils.CollectionUtil;
import com.metro.utils.EncryptUtil;
import com.metro.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.sys.mapper.ISysUserMapper;
import com.metro.paperless.sys.service.inf.ISysUserService;
import com.metro.paperless.sys.beans.vo.SysUser;

import java.util.Date;
import java.util.List;

 /**
 * 系统用户业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysUserService")
public class SysUserService extends BaseServiceSupport<SysUser> implements ISysUserService{
    //系统用户Mapper
    @Autowired
    private ISysUserMapper sysUserMapper;
    @Autowired
    private ISysUserRoleMapper sysUserRoleMapper;

    @Override
    public IMapper getMapper() {
        return sysUserMapper;
    }

     @Override
     public SysUser getSysUserByAccount(String account) {
         SysUser queryEntity = new SysUser();
         queryEntity.setAccount(account);
         QueryParams<SysUser> params = new QueryParams<>();
         params.setEntity(queryEntity);
         List<SysUser> list = sysUserMapper.queryForList(params);
         if(list != null && list.size() > 0){
             return list.get(0);
         }
         return null;
     }

     @Override
     public IResult updatePassword(SysUser entity) {
         try {
             entity.setId(super.getCurrUserId());
             sysUserMapper.updateNotEmpty(entity);
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("修改密码失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }


     @Override
     public IResult saveUserLoginInfo(SysUser user) {
         try {
             user.setId(this.getCurrUserId());
             sysUserMapper.updateNotEmpty(user);
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("新增系统用户失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

    @Override
    public IResult save(SysUser entity) {
    	//用户创建时间
    	Date date = new Date();
    	entity.setAddTime(date);
    	
        try {
            if(StringUtil.isEmpty(entity.getPassword())){
                entity.setPassword(PropertiesEnum.getPropertyValue(PropertiesEnum.SYSTEM_DEFAULT_PASSWORD));
            }
            entity.setPassword(EncryptUtil.md5(entity.getPassword()));
            sysUserMapper.save(entity);
            //保存用户角色信息
            List<SysUserRole> userRoles = entity.buildUserRoleList();
            if(CollectionUtil.isNotEmpty(userRoles)){
                sysUserRoleMapper.batchSaveUserRole(userRoles);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增系统用户失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysUser entity) {
        try {
            if(StringUtil.isNotEmpty(entity.getPassword())){
                entity.setPassword(EncryptUtil.md5(entity.getPassword()));
            }
            sysUserMapper.update(entity);
            //保存用户角色信息
            List<SysUserRole> userRoles = entity.buildUserRoleList();
            if(CollectionUtil.isNotEmpty(userRoles)){
                sysUserRoleMapper.deleteByUserId(entity.getId());
                sysUserRoleMapper.batchSaveUserRole(userRoles);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改系统用户失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysUserMapper.delete(id);
            sysUserRoleMapper.deleteByUserId(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysUserMapper.deleteByIds(ids);
            List<Long> idList = StringUtil.split2LongList(ids);
            for (Long item : idList){
                sysUserRoleMapper.deleteByUserId(item);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}